#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        
        std::string res;
        for (int i = 0; i < numRows; ++i) {
            int gap1 = 2 * (numRows - 1);
            int gap2 = 2 * (numRows - i - 1);

            for (int j = i; j < s.size(); j += gap1) {
                res += s[j];
                if (!(gap2 == gap1 || gap2 == 0) && j + gap2 < s.size()) {
                    res += s[j + gap2];
                }
            }
        }
        return res;
    }
};

int main(int argc, char *argv[]) {
    std::cout << Solution().convert("PAYPALISHIRING", 4) << std::endl;
    return 0;
}